package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.builder.vo.ExhibitorVO;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.*;
import com.eastfair.venueservice.entity.ServiceProvider;
import com.eastfair.venueservice.service.ExhibitionServiceProviderService;
import com.eastfair.venueservice.service.ServiceProviderService;
import com.eastfair.venueservice.vo.ExhibitionServiceProviderVO;
import com.eastfair.venueservice.vo.ServiceProviderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务商信息表
 * </p>
 *
 * @author gj
 * @date 2022 -07-13
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceProvider")
@Api(value = "ServiceProvider", tags = "服务商信息表")
public class ServiceProviderController extends SuperController<ServiceProviderService, Long, ServiceProvider, ServiceProviderPageQuery, ServiceProviderSaveDTO, ServiceProviderUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ServiceProviderService serviceProviderService;

    @Resource
    private ExhibitionServiceProviderService exhibitionServiceProviderService;

    @Override
    public void handlerResult(IPage<ServiceProvider> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ServiceProvider> serviceProviderList = list.stream()
                .map((map) -> ServiceProvider.builder().build()).collect(Collectors.toList());

        return R.success(baseService.saveBatch(serviceProviderList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceProviderPageQuery> params) {
        ServiceProviderPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询服务商列表.
     *
     * @param serviceProviderDTO the page params
     * @return the r
     */
    @GetMapping("/page/serviceProvider")
    public R<IPage<ServiceProviderVO>> getServiceProviderList(ServiceProviderDTO serviceProviderDTO) {
        log.info("getServiceProviderList - 分页查询服务商列表, serviceProviderDTO={}", serviceProviderDTO);
        try {
            PageParams<ServiceProviderDTO> pageParams = new PageParams<>();
            pageParams.setModel(serviceProviderDTO);
            pageParams.setCurrent(serviceProviderDTO.getCurrent());
            pageParams.setSize(serviceProviderDTO.getSize());
            pageParams.setSort(serviceProviderDTO.getSort());
            pageParams.setOrder(serviceProviderDTO.getOrder());
            return R.successDef(serviceProviderService.listServiceProviderVOs(pageParams));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 分页查询服务商列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/serviceProvider")
    public R<IPage<ServiceProviderVO>> queryServiceProvider(@RequestBody PageParams<ServiceProviderDTO> pageParams) {
        log.info("queryServiceProvider - 分页查询服务商列表, pageParams={}", pageParams);
        try {
            return R.successDef(serviceProviderService.listServiceProviderVOs(pageParams));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 查询服务商id集合.
     *
     * @param params the params
     * @return the r
     */
    @PostMapping("/queryServiceProviderIds")
    public R<List<Long>> queryServiceProviderIds(@RequestBody ServiceProviderDTO params) {
        log.info("queryServiceProviderIds - 查询服务商id集合, params={}", params);
        try {
            return R.successDef(serviceProviderService.queryServiceProviderIds(params));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 分页查询服务商列表.
     *
     * @param query the page params
     * @return the r
     */
    @PostMapping("/query/serviceProvider")
    public R<List<ServiceProviderVO>> queryServiceProvider(@RequestBody ServiceProviderDTO query) {
        log.info("queryServiceProvider - 分页查询服务商列表, pageParams={}", query);
        try {
            return R.successDef(serviceProviderService.listServiceProviderVOs(query));
        } catch (BizException e) {
            return R.fail(e);
        }
    }


    /**
     * cms同步查询服务商列表.
     *
     * @return
     */
    @PostMapping("/cms/sync")
    public R<List<ServiceProviderVO>> queryCrmSyncServiceProvider() {
        try {
            return R.successDef(serviceProviderService.queryCrmSyncServiceProvider());
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 主场查询负责的搭建商.
     *
     * @param homeId             the home id
     * @param exhibitionManageId the exhibition manage id
     * @return the r
     */
    @PostMapping("/query/builder/exhibitor")
    public R<List<ExhibitorVO>> queryBuilderByHomeId(@RequestParam("homeId") Long homeId,
                                                     @RequestParam("exhibitionManageId") Long exhibitionManageId) {
        log.info("queryBuilderByHomeId - 主场查询负责的搭建商, homeId={}, exhibitionManageId={}", homeId, exhibitionManageId);
        try {
            return R.successDef(serviceProviderService.listBuilderByHomeId(homeId, exhibitionManageId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * id查询服务商详情.
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/get/{id}")
    public R<ServiceProviderVO> getById(@PathVariable("id") Long id) {
        log.info("getById - 用户ID查询服务商信息, id={}", id);
        try {
            return R.successDef(serviceProviderService.getServiceProviderVOById(id));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 用户ID查询服务商信息.
     *
     * @param userId              the user id
     * @param serviceProviderType the service provider type
     * @return the by user id
     */
    @GetMapping("/user/get")
    public R<ServiceProviderVO> getByUserId(@RequestParam("userId") Long userId,
                                            @RequestParam("serviceProviderType") String serviceProviderType) {
        log.info("getByUserId - 用户ID查询服务商信息, userId={}, serviceProviderType={}", userId, serviceProviderType);
        try {
            return R.successDef(serviceProviderService.getServiceProviderVOByUserId(userId, serviceProviderType));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 提交服务商信息.
     *
     * @param serviceProviderDTO the service provider dto
     * @return the r
     */
    @SysLog("提交服务商")
    @PostMapping("/submit")
    public R<Boolean> submitServiceProvider(@RequestBody @Validated({ServiceProviderDTO.Save.class})
                                            ServiceProviderDTO serviceProviderDTO) {
        log.info("submitServiceProvider - 提交服务商信息, serviceProviderDTO={}", serviceProviderDTO);
        try {
            return R.success(serviceProviderService.submitServiceProvider(serviceProviderDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 新增服务商信息.
     *
     * @param serviceProviderDTO the service provider dto
     * @return the r
     */
    @Deprecated
    @PostMapping("/save")
    public R<Boolean> saveServiceProvide(@RequestBody ServiceProviderDTO serviceProviderDTO) {
        log.info("saveServiceProvide - 新增服务商, serviceProviderDTO={}", serviceProviderDTO);
        try {
            serviceProviderService.saveServiceProvider(serviceProviderDTO);
            return R.successDef(true);
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新服务商.
     *
     * @param serviceProviderDTO the service provider dto
     * @return the r
     */
    @SysLog("修改服务商")
    @PostMapping("/update")
    public R<Boolean> updateServiceProvide(@RequestBody ServiceProviderDTO serviceProviderDTO) {
        log.info("updateServiceProvide - 更新服务商, serviceProviderDTO={}", serviceProviderDTO);
        try {
            return R.successDef(serviceProviderService.updateServiceProvider(serviceProviderDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新服务商为已失效.
     *
     * @return the r
     */
    @GetMapping("/update/disable")
    public R<Boolean> updateServiceProviderDisable(@RequestParam("serviceProviderType") String serviceProviderType) {
        log.info("updateServiceProviderDisable - 更新服务商为已失效,serviceProviderType={}", serviceProviderType);
        try {
            return R.successDef(serviceProviderService.updateServiceProviderDisableByType(serviceProviderType));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 更新服务商crm同步状态为已同步.
     *
     * @param ids the ids
     * @return the r
     */
    @PostMapping("/update/crm/sync")
    public R<Boolean> updateCrmHasSync(@RequestBody List<Long> ids) {
        log.info("updateCrmHasSync - 更新服务商crm同步状态为已同步, ids={}", ids);
        return R.successDef(serviceProviderService.updateCrmHasSync(ids));
    }

    /**
     * 查询展会服务商
     *
     * @param query 查询展会服务商
     * @return R
     */
    @ApiOperation(value = "查询展会服务商", notes = "查询展会服务商")
    @PostMapping("/queryExhibitionServiceProvider")
    public R<List<ServiceProviderVO>> queryExhibitionServiceProvider(@RequestBody ExhibitionServiceProviderQuery query) {
        log.info("queryExhibitionServiceProvider - 查询展会服务商 query={}", query);
        return R.success(exhibitionServiceProviderService.queryExhibitionServiceProvider(query));
    }

    /**
     * 展会ID获取负责展馆的服务商.
     *
     * @param exhibitionId        the exhibition id
     * @param spaceCode           the space code
     * @param serviceProviderType the service provider type
     * @return the service provider vo map by exhibition id
     */
    @GetMapping("/get/serviceProvider/map")
    public R<Map<String, List<ServiceProviderVO>>> getServiceProviderVOMapByExhibitionId(@RequestParam("exhibitionId") Long exhibitionId,
                                                                                         @RequestParam("spaceCodeList") List<String> spaceCode,
                                                                                         @RequestParam("serviceProviderType") String serviceProviderType) {
        log.info("getServiceProviderVOMapByExhibitionId - 展会ID获取负责展馆的服务商, exhibitionId={}, spaceCode={}, serviceProviderType={}", exhibitionId, spaceCode, serviceProviderType);
        try {
            return R.successDef(exhibitionServiceProviderService.getServiceProviderVOMapByExhibitionId(exhibitionId, spaceCode, serviceProviderType));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 展会ID和展厅编码查询负责的主场.
     *
     * @param exhibitionId the exhibition id
     * @param spaceCode    the space code
     * @return the home by exhibition id and space code
     */
    @GetMapping("/get/home")
    public R<ServiceProviderVO> getHomeByExhibitionIdAndSpaceCode(@RequestParam("exhibitionId") Long exhibitionId,
                                                                  @RequestParam("spaceCode") String spaceCode) {
        log.info("getHomeByExhibitionIdAndSpaceCode- 展会ID和展厅编码查询负责的主场, exhibitionId={}, spaceCode={}", exhibitionId, spaceCode);
        try {
            return R.successDef(exhibitionServiceProviderService.getHomeByExhibitionIdAndSpaceCode(exhibitionId, spaceCode));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 展会ID查询负责展会的主场
     *
     * @param exhibitionId the exhibition id
     * @return the home by exhibition id
     */
    @GetMapping("/get/exhibition/home/{exhibitionId}")
    public R<ServiceProviderVO> getHomeByExhibitionId(@PathVariable("exhibitionId") Long exhibitionId) {
        log.info("getHomeByExhibitionId - 展会ID查询负责展会的主场, exhibitionId={}", exhibitionId);
        try {
            return R.successDef(exhibitionServiceProviderService.getHomeByExhibitionId(exhibitionId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 查询展会未同步的所有展会服务商关系
     *
     * @return R
     */
    @ApiOperation(value = "查询展会未同步的所有展会服务商关系", notes = "查询展会未同步的所有展会服务商关系")
    @PostMapping("/queryNeedSyncExhibitionProvider")
    public R<List<ExhibitionServiceProviderVO>> queryNeedSyncExhibitionProvider() {
        log.info("queryNeedSyncExhibitionProvider - 查询展会未同步的所有展会服务商关系");
        return R.success(baseService.queryNeedSyncExhibitionProvider());
    }
}
